package com.dkm.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Module;
import com.dkm.service.ModuleService;
import com.dkm.service.TreeService;
import com.dkm.util.ResultUtil;
import com.dkm.util.TreeNode;

@RestController
@RequestMapping("/module")
public class ModuleController {

	//注入模块业务层
	@Autowired
	private ModuleService moduleService;
	//注入树业务层
	@Autowired
	private TreeService treeService;
	
	//查询全部模块信息
	@GetMapping
	@RequiresPermissions(value = "module:query")
	public List<TreeNode> findAllModules() {
		//获取全部的模块信息
		List<Module> modules=moduleService.findAllModules();
		//转换为树结构
		return treeService.getLayuiModuleTree(modules);
	}
	
	//根据模块id获取模块信息
	@GetMapping("/getModuleByModule_id")
	@RequiresPermissions(value = "module:getModuleByModuleId")
	public Module getModuleByModule_id(Integer module_id) {
		
		return moduleService.getModuleByModule_id(module_id);
	}
	//新增模块
	@PostMapping
	@RequiresPermissions(value = "module:add")
	public ResultUtil addModule(Module module) {
		
		return moduleService.saveAndFlushModule(module);
	}
	//修改模块
	@PutMapping
	@RequiresPermissions(value = "module:update")
	public ResultUtil updateModule(Module module) {
		
		return moduleService.saveAndFlushModule(module);
	}
	
	//删除模块
	@DeleteMapping
	@RequiresPermissions(value = "module:delete")
	public ResultUtil deleteModule(Integer module_id) {
		
		return moduleService.deleteModule(module_id);
	}
	
	@GetMapping("/getAllModules")
	public ResultUtil getAllModules() {
		
		List<Module> modules=moduleService.findAllModules();
		
		if(modules!=null&&modules.size()>0) {
			return new ResultUtil(0, "模块数据获取成功", modules);
		}
		return new ResultUtil(-1, "模块数据获取失败", null);
	}
}
